﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Common;
using StoragePeriod.Web.Common;

namespace StoragePeriod.Web.SysManage
{
    public partial class SenseTypeManage : SecurityPage
    {
        #region 属性

        #endregion

        #region 继承
        public override string PrivCode()
        {
            return "7abf6bb086a782fc";
        }
        #endregion

        #region 函数

        private void DataBing()
        {
            if (this.Drop_RawType.SelectedItem != null)
            {
                IQueryable<Grading> RawType = Grading.Where("temp1=@0", this.Drop_RawType.SelectedValue);
                this.RepDataList.DataSource = RawType;
                this.RepDataList.DataBind();
            }
        }

        private void DropBing()
        {
            List<RawMaterialType> list = RawMaterialType.Where("ParentId=@0 and Typeinfoid=@1", 0, Convert.ToInt32(StoragePeriod.Data.EnumData.TypeInfo.食品检测)).ToList();
            this.Drop_RawType.DataSource = list;
            this.Drop_RawType.DataTextField = "TypeName";
            this.Drop_RawType.DataValueField = "id";
            this.Drop_RawType.DataBind();

            if (this.Drop_RawType.Items.Count > 0)
            {
                this.Drop_RawType.Items[0].Selected = true;
                this.Drop_RawType_SelectedIndexChanged(null, null);
            }
        }

        int i = 0;
        public string GetXH()
        {
            i += 1;
            return i.ToString();
        }

        #endregion

        #region 事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            DropBing();
            //DataBing();
        }

        protected void RepDataList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int RawTypeId = Convert.ToInt32(e.CommandArgument);
                Grading md=Grading.Where("id=@0", RawTypeId).First();
                string strTypeName = md.GradingName;
                string strContents = md.GradingContent;
                this.HidTypeID.Value = RawTypeId.ToString();
                UtilityUI.Load(this.upPanelMain, "LoadData('" + strTypeName + "','" + strContents + "');ShowDlg(1)");
            }
            else
            {
                int RawTypeId = Convert.ToInt32(e.CommandArgument);
                Grading.Delete("id=@0", RawTypeId);
                DataBing();
                UtilityUI.Load(this.upPanelMain, "alertstr('删除成功！')");
            }
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            if (this.HidIsEdit.Value == "0")
            {
                //添加
                Grading md = new Grading();
                md.GradingName = this.txt_GradingName.Text.Trim();
                md.GradingContent = this.txt_GradingContent.Text.Trim();
                md.temp1 = this.Drop_RawType.SelectedValue;
                Grading.Insert(md);
                DataBing();
            }
            else
            {
                //修改
                int RawTypeId = Convert.ToInt32(this.HidTypeID.Value);
                Grading md = Grading.Where("id=@0", RawTypeId).First();
                md.GradingName = this.txt_GradingName.Text.Trim();
                md.GradingContent = this.txt_GradingContent.Text.Trim();
                Grading.Update(md, "id=@0", RawTypeId);
                DataBing();
            }
        }

        protected void Drop_RawType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.DataBing();
        }

        #endregion

        
    }
}